package com.yanqu.road.server.cmd.union;

import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.union.UnionJoinApplyMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

//@Cmd(code = Protocol.S_UNION_DEAL_APPLY, desc = "处理加入申请")
public class DealJoinApplyCmd extends AbstractCommand {

	private boolean isAllRefuse;

	private long applyUserId;

	private boolean isAgree;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		UnionProto.UnionDealJoinApplyReqMsg reqMsg = UnionProto.UnionDealJoinApplyReqMsg.parseFrom(packet.getBytes());
		isAllRefuse = reqMsg.getIsAllRefuse();
		applyUserId = reqMsg.getUserId();
		isAgree = reqMsg.getIsAgree();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		// 判断权限 族长 副族长才有权限
		String unionUid = player.getUserInfo().getUnionUid();
		if(StringUtils.isNullOrEmpty(unionUid)){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		UnionMember member = UnionMgr.getUnionMember(unionUid, player.getUserId());
		if(null == member){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		if (member.getPosition() == eUnionPosition.Master.getValue() || member.getPosition() == eUnionPosition.DeputyMaster.getValue()) {
			if (isAllRefuse) {// 一键拒绝
				UnionJoinApplyMgr.removeAllJoinApply(unionUid);
			} else {
				if (isAgree) {
					ret = UnionJoinApplyMgr.agreeJoinApply(player, unionUid, applyUserId);
				} else {
					ret = UnionJoinApplyMgr.removeJoinApply(unionUid, applyUserId);
				}
			}
		} else {
			ret = GameErrorCode.E_UNION_POSITION_NO_ENOUGH;
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		UnionProto.UnionDealJoinApplyRespMsg.Builder respMsg = UnionProto.UnionDealJoinApplyRespMsg.newBuilder();
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_UNION_DEAL_APPLY, respMsg);
	}

}
